<?php


namespace app\index\controller;


use app\common\model\User;
use come\Captcha;
use come\Log;
use come\Session;

class UserController extends IndexBase
{
    function loginAction(){
        return $this->success("哈哈");
    }

    function adminLoginAction(){
        if($this->request->isPost()){
            if($this->request->isEmpty(['username','password','captcha'])){
                return $this->error("参数缺失");
            }

            if(!Captcha::check($this->request->param("captcha"))){
                return $this->error("验证码不正确");
            }

            $username = $this->request->param("username");
            $password = $this->request->param("password");

            $user = User::find()->where(['username'=>$username,'password'=>md5($password)])->getOne();
            if(empty($user['uid'])){
                return $this->error("账号或密码错误");
            }else{
                Session::set("session_user",$user->toArray());
                Log::addLog($user->toArray());
                Log::addLog("session_user");
                Log::addLog(Session::get("session_user"));
                return $this->success("登录成功！");
            }
        }else{
            return view();
        }
    }
}